package com.example.dbcms.service.impl;

import com.example.dbcms.domain.Parking;
import com.example.dbcms.domain.Residents;
import com.example.dbcms.mapper.ParkingDao;
import com.example.dbcms.mapper.ParkingFeeInfoDao;
import com.example.dbcms.mapper.PropertyFeeDao;
import com.example.dbcms.mapper.ResidentDao;
import com.example.dbcms.service.ParkingFeeService;
import com.example.dbcms.service.ParkingService;
import com.example.dbcms.service.PropertyFeeService;
import com.example.dbcms.service.TimedTaskService;
import com.example.dbcms.utils.DateUtil;
import com.example.dbcms.utils.ResultVoUtil;
import com.example.dbcms.utils.ToolUtil;
import com.example.dbcms.vo.ResultVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class TimedTaskServiceImpl implements TimedTaskService {

    @Autowired
    private ResidentDao residentDao;

    @Autowired
    private PropertyFeeDao propertyFeeDao;

    @Autowired
    private PropertyFeeService propertyFeeService;

    @Autowired
    private ParkingDao parkingDao;

    @Autowired
    private ParkingFeeInfoDao parkingFeeInfoDao;

    @Autowired
    private ParkingFeeService parkingFeeService;

    @Override
    public ResultVo refreshPropertyFee() {
        List<Residents> residents = residentDao.selectAllResidents(null);//得到所有居民信息
        for (int i=0;i<residents.size();i++){
            Residents resident = residents.get(i);
            Date date = DateUtil.changeStringToDate(resident.getCheckinDate());
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH)+1;
            //System.out.println(resident.getUserId()+"---"+year+"-----"+month);
            for (int j =year;j<=DateUtil.getCurrentYear();j++){
                if(j==DateUtil.getCurrentYear()&&j==year){
                    for(int k=month;k<=DateUtil.getCurrentMonth();k++){
                        if(propertyFeeDao.getPropertyFeeByYearAndMonth(resident.getUserId(),j,k)==null){
                            //System.out.println("缺少"+resident.getUserId()+"--"+j+"--"+k);
                           // System.out.println(ToolUtil.getRandomString(20));
                            int result = propertyFeeService.addPropertyFee(resident.getUserId(), j, k, resident.getPropertyFee());
                            if(result!=1){
                                return ResultVoUtil.error("刷新失败");
                            }
                        }
                    }
                }else if(j==DateUtil.getCurrentYear()){
                    for(int k=1;k<=DateUtil.getCurrentMonth();k++){
                        if(propertyFeeDao.getPropertyFeeByYearAndMonth(resident.getUserId(),j,k)==null){
                            //System.out.println("缺少"+resident.getUserId()+"--"+j+"--"+k);
                            // System.out.println(ToolUtil.getRandomString(20));
                            int result = propertyFeeService.addPropertyFee(resident.getUserId(), j, k, resident.getPropertyFee());
                            if(result!=1){
                                return ResultVoUtil.error("刷新失败");
                            }
                        }
                    }
                } else if(j==year){
                    for(int k=month;k<=12;k++){
                        if(propertyFeeDao.getPropertyFeeByYearAndMonth(resident.getUserId(),j,k)==null){
                            //System.out.println("缺少"+resident.getUserId()+"--"+j+"--"+k);
                            int result = propertyFeeService.addPropertyFee(resident.getUserId(), j, k, resident.getPropertyFee());
                            if(result!=1){
                                return ResultVoUtil.error("刷新失败");
                            }
                        }
                    }
                }else {
                    for(int k=1;k<=12;k++){
                        if(propertyFeeDao.getPropertyFeeByYearAndMonth(resident.getUserId(),j,k)==null){
                           // System.out.println("缺少"+resident.getUserId()+"--"+j+"--"+k);
                            int result = propertyFeeService.addPropertyFee(resident.getUserId(), j, k, resident.getPropertyFee());
                            if(result!=1){
                                return ResultVoUtil.error("刷新失败");
                            }
                        }
                    }
                }
            }
        }
        return ResultVoUtil.success("刷新成功");
    }

    @Override
    public ResultVo refreshParkingFee() {
        List<Parking> parkingList = parkingDao.selectAllParking(null);//获得所有停车位信息
        for (int i=0;i<parkingList.size();i++){
            Parking parking = parkingList.get(i);
            if(parking.getSale().equals("是")){
                Date date = DateUtil.changeStringToDate(parking.getBuyDate());
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                int year = calendar.get(Calendar.YEAR);
                int month = calendar.get(Calendar.MONTH)+1;
                for (int j =year;j<=DateUtil.getCurrentYear();j++){
                    if(j==DateUtil.getCurrentYear()&&j==year){
                        for (int k=1;k<=DateUtil.getCurrentMonth();k++){
                            if(parkingFeeInfoDao.getParkingFeeByYearAndMonth(parking.getParkingId(),j,k)==null){
                                int result = parkingFeeService.addParkingFee(parking.getParkingId(), j, k, parking.getParkingFee());
                                if(result!=1){
                                    return ResultVoUtil.error("刷新失败");
                                }
                            }
                        }
                    }else if(j==DateUtil.getCurrentYear()){
                        for (int k=1;k<=DateUtil.getCurrentMonth();k++){
                            if(parkingFeeInfoDao.getParkingFeeByYearAndMonth(parking.getParkingId(),j,k)==null){
                                int result = parkingFeeService.addParkingFee(parking.getParkingId(), j, k, parking.getParkingFee());
                                if(result!=1){
                                    return ResultVoUtil.error("刷新失败");
                                }
                            }
                        }
                    } else if(j==year){
                        for (int k=month;k<=12;k++){
                            if(parkingFeeInfoDao.getParkingFeeByYearAndMonth(parking.getParkingId(),j,k)==null){
                                int result = parkingFeeService.addParkingFee(parking.getParkingId(), j, k, parking.getParkingFee());
                                if(result!=1){
                                    return ResultVoUtil.error("刷新失败");
                                }
                            }
                        }
                    }else {
                        for (int k=1;k<=12;k++){
                            if(parkingFeeInfoDao.getParkingFeeByYearAndMonth(parking.getParkingId(),j,k)==null){
                                int result = parkingFeeService.addParkingFee(parking.getParkingId(), j, k, parking.getParkingFee());
                                if(result!=1){
                                    return ResultVoUtil.error("刷新失败");
                                }
                            }
                        }
                    }
                }
            }
        }
        return ResultVoUtil.success("刷新成功");
    }
}
